// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import API_TOTAL from './API_TOTAL'
import API_MAP from './API_MAP'
import API_DEVICE from './API_DEVICE'
import API_ACCESS from './door/API'
import API_PARKING from './park/API'

// ============================== 导出异步调用 ============================== //

export default {
  // —————————————————————————————— 信息 —————————————————————————————— //

  async remoteInfo() {
    const total = await API_TOTAL.getInfo()

    const point = await API_MAP.getList()
    point.push(BJ)

    point.forEach((item) => {
      item.site = JSON.parse(item.siteJson || '{}')
    })

    return { total, point }
  },

  // —————————————————————————————— 列表 —————————————————————————————— //

  async remoteList({ state }) {
    if (state.infoType === 'door') {
      const [num, arr] = await API_ACCESS.getList(state.param)
      return [num, transDoor(arr)]
    }

    if (state.infoType === 'park') {
      const [num, arr] = await API_PARKING.getList(state.param)
      return [num, transPark(arr)]
    }
  },

  // —————————————————————————————— 区域的设备列表 —————————————————————————————— //

  async remoteSelect({ state: { selectParam, DICT } }) {
    if (!selectParam.areaId) return

    const multi = DICT.SUB_SYSTEM.map(([subSystemId, systemName]) => {
      return API_DEVICE.getList({ ...selectParam, subSystemId }, `查询设备列表：${systemName}`)
    })
    const select = await Promise.all(multi)
    return select
  },
}

// ============================== 转换方法 ============================== //

const transDoor = (arr) => {
  arr.forEach((item) => {
    item.name = CORE.maskName(item.name)
    item.mobile = CORE.maskPhone(item.phone)
    item.time = CORE.timeStr(item.accessTime)
    item.img = CORE.image(item.captureData)
  })
  return arr
}

const transPark = (arr) => {
  arr.forEach(item => {
    item.name = item.plateNo
    item.time = CORE.timeStr(item.recordTime)
    item.img = CORE.image(item.vehiclePicUrl)
  })
  return arr
}

const BJ = {
  areaId: '681197085282185216',
  areaMapId: '1',
  areaName: '测试',
  createBy: '0d6608b8012d4fbfb460217c0718c983',
  createTime: 1646980427765,
  deviceCategoryId: '1',
  deviceId: '692064776438575144',
  deviceName: '告警点1',
  elevation: '88.57688903808594',
  id: '699668468637179903',
  latitude: '30.151304932861308',
  longitude: '120.16666048937978',
  objectType: 1,
  siteJson: '{"layer":"YueShengGJ","bind":"30Ceng","EPSGType":"4326","zone":0,"floor":30,"icon":"YS_bj"}',
  subSystemId: '3',
  tenantNo: 1,
  updateBy: '0d6608b8012d4fbfb460217c0718c983',
  updateTime: 1646894825126,
}
